<template>
  <div>
    <div class="permissManaTool">
      <el-input placeholder="请输入内容" v-model="role.name">
        <template #prepend>ROLE_</template>
      </el-input>
      <el-input v-model="role.nameZh" placeholder="请输入角色中文名"></el-input>
      <el-button type="primary" icon="el-icon-plus" @click="doAddRole">添加角色</el-button>
    </div>
    <div class="permissManaMain">
      <el-collapse v-model="activeName" accordion @change="change">
        <el-collapse-item :title="r.nameZh" :name="r.id" v-for="(r, index) in roles">
          <el-card class="box-card">
            <div class="card-header">
              <span>卡片名称</span>
              <el-button
              size="small"
              style="float: right; color: #ff00000" 
              class="button" 
              type="danger"
              icon="el-icon-delete"
              @click="doDeleRole(r)"> 删除</el-button>
            </div>
            <div>
              <el-tree
                show-checkbox :data="allMenus"
                :props="defaultProps"
                :default-checked-keys="selectedMenus"
                node-key="id"
                :key="index"
                ref="tree"
                ></el-tree>
                <div style="margin: 15px 0px; float: right;">
                  <el-button size="small">取消修改</el-button>
                  <el-button size="small" type="primary" @click="doUpdata(r.id, index)">确认修改</el-button>
                </div>
            </div>
          </el-card>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      role: {
        name: '',
        nameZh: ''
      },
      activeNames: 1,
      roles: [],
      allMenus: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      selectedMenus: [],
      activeName: -1
    }
  },
  created () {
     this.initRoles()
  },
  methods: {
    doDeleRole (data) {
      console.log(data)
      this.deleteRequest('/system/basic/permiss/role/' + data.id).then(resp => {
        if(resp) {
          console.log(resp)
          this.initRoles()
        }
      })
    },
    doAddRole () {
      if ( this.role.name && this.role.nameZh) {
        this.postRequest('/system/basic/permiss/role', this.role).then(resp => {
          this.initRoles()
          this.role.name = ''
          this.role.nameZh = ''
        })
      } else {
        alert("不能为空")
      }
    },
    doUpdata (rid, index) {
      let tree = this.$refs.tree[index]
      let selectedKeys = tree.getCheckedKeys(true);
      let url = '/system/basic/permiss/?rid=' + rid
      selectedKeys.forEach(key => {
        url += '&mids=' + key;
      });
      this.putRequest(url).then(resp => {
        if (resp) {
          this.activeName = -1
        }
      })
    },
    initSelectMenus (rid) {
      this.getRequest('/system/basic/permiss/mid/' + rid).then(resp => {
        if (resp) {
          this.selectedMenus = resp
        }
      })
    },
     initRoles() {
       this.getRequest('/system/basic/permiss/').then(resp => {
         if (resp) {
           this.roles = resp
         }
       })
     },
     change (rid) {
       if (rid) {
        this.initAllMenus()
        this.initSelectMenus(rid)
       }
     },
     initAllMenus () {
       this.getRequest('/system/basic/permiss/menus').then(resp => {
         if (resp) {
           this.allMenus = resp
         }
       })
     }
  }
}
</script>

<style>
.permissManaTool {
  display: flex;
}
.permissManaTool .el-input {
  width: 300px;
  margin-right: 15px;
}
.permissManaMain {
  margin-top: 15px;
  width: 1000px;
}
.card-header {
  margin-bottom: 15px;
}
</style>
